package com.zzedu.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzedu.common.utils.StringUtils;
import com.zzedu.system.domain.SysOperationLog;
import com.zzedu.system.mapper.SysOperationLogMapper;
import com.zzedu.system.service.ISysOperationLogService;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 操作日志 服务层处理
 *
 * @author smarthome
 */
@Service
public class SysOperationLogServiceImpl extends ServiceImpl<SysOperationLogMapper, SysOperationLog> implements ISysOperationLogService {

    /**
     * 查询系统操作日志集合
     *
     * @param operLog 操作日志对象
     * @return 操作日志集合
     */
    @Override
    public IPage<SysOperationLog> selectOperLogList(IPage<SysOperationLog> page, SysOperationLog operLog) {
        LambdaQueryWrapper<SysOperationLog> queryWrapper = Wrappers.lambdaQuery(SysOperationLog.class);
        queryWrapper.like(StringUtils.isNotBlank(operLog.getTitle()), SysOperationLog::getTitle, operLog.getTitle());
        queryWrapper.eq(StringUtils.isNotNull(operLog.getBusinessType()), SysOperationLog::getBusinessType, operLog.getBusinessType());
        queryWrapper.in(StringUtils.isNotNull(operLog.getBusinessTypes()), SysOperationLog::getBusinessType, operLog.getBusinessTypes());
        queryWrapper.eq(StringUtils.isNotNull(operLog.getStatus()), SysOperationLog::getStatus, operLog.getStatus());
        queryWrapper.like(StringUtils.isNotBlank(operLog.getOperName()), SysOperationLog::getOperName, operLog.getOperName());
        Map map = operLog.getParams();
        if (!MapUtils.isEmpty(map)) {
            if (map.containsKey("beginTime")) {
                queryWrapper.ge(SysOperationLog::getOperTime, MapUtils.getString(map, "beginTime"));
            }
            if (map.containsKey("endTime")) {
                queryWrapper.le(SysOperationLog::getOperTime, MapUtils.getString(map, "endTime"));
            }
        }
        queryWrapper.orderByDesc(SysOperationLog::getId);
        return page(page, queryWrapper);
    }

    /**
     * 清空操作日志
     */
    @Override
    public void cleanOperLog() {
        baseMapper.cleanOperLog();
    }
}
